<template>
  <div class="departments">
    <div class="app-container">
      <el-card
        shadow="always"
        :body-style="{padding:'20px'}"
      >
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ company.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row
                type="flex"
                justify="end"
              >
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="openAddDept()">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- default-expand-all 默认展开所有节点 -->
        <el-tree
          :data="treeData"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <el-row style="width: 100%;">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row
                  type="flex"
                  justify="end"
                >
                  <!-- 两个内容 -->
                  <el-col>-----</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openAddDept(data,1)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="openAddDept(data,2)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDept(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </template>
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="ad"
      :show-dialog="showDialog"
      :parent-dept="parentDept"
      :all-depts="allDepts"
      @close-dialog="handleClose"
      @refresh-dept="getTreeData"
    />
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments.js'
import { tranformTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
/**
 * 开发页面流程
 * 1、在api目录封装当前页面后台接口方法
 * 2、导入需要使用的后台接口方法
 * 3、data中定义存储后台看数据的变量
 * 4、在methods中定义获取后台数据的方法
 * 5、在created / mounted钩子函数中发请求获取数据并存储到data变量中
 * 6、在template中绑定变量或事件（功能交互）
 */
export default {
  components: {
    AddDept
  },
  data () {
    return {
      allDepts: [],
      showDialog: false,
      // 公司部门数据（树形结构）
      treeData: [],
      company: { name: '' },
      defaultProps: {
        children: 'children',
        // 指定渲染使用的部门属性名叫name
        label: 'name'
      },
      parentDept: null
    }
  },
  created () {
    this.getTreeData()
  },
  methods: {
    // 获取部门数据
    async getTreeData () {
      const { depts, companyName } = await getDepartments()
      this.treeData = tranformTreeData(depts)
      this.allDepts = depts
      this.company.name = companyName
    },
    // 要删除的部门数据
    async delDept (curDept) {
      /**
       * 删除思路
       * 1、用户确认
       * 2、确认之后 => 判断是否父部门，如果是不能删除
       * 3、如果没有子部门就可以删除
       * 4、重新获取数据
       */
      try {
        await this.$confirm(`确认删除：${curDept.name}吗`)
        if (curDept.children && curDept.children.length > 0) {
          return this.$message.error('不能直接删除父部门')
        }
        await delDepartments(curDept.id)
        this.getTreeData()
        this.$message.success('删除成功')
      } catch (error) {
        // 点击取消或代码写错
        console.log(error)
      }
    },
    // 打开新增部门弹层
    openAddDept (parentDept, type) {
      console.log(parentDept)
      this.parentDept = parentDept
      this.showDialog = true
      if (type === 2) {
        this.$refs.ad.getDeptDetil(parentDept.id)
      }
    },
    handleClose (flag) {
      this.showDialog = flag
    }
  }
}
</script>

<style lang="scss" scoped>
// 深度作用选择符   lass使用 /deep/   | scss使用 ::v-deep
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
